package leetcode.code0133;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;

public class Solution implements IDebug {

	class Node {
		public int val;
		public List<Node> neighbors;

		public Node() {
			val = 0;
			neighbors = new ArrayList<>();
		}

		public Node(int _val) {
			val = _val;
			neighbors = new ArrayList<>();
		}

		public Node(int _val, ArrayList<Node> _neighbors) {
			val = _val;
			neighbors = _neighbors;
		}
	}

	public Node cloneGraph(Node node) {
		Node[] hash = new Node[101];
		return clone(node, hash);
	}

	private Node clone(Node node, Node[] hash) {
//		if (node == null) {
//			return null;
//		}
		if (hash[node.val] != null) {
			return hash[node.val];
		}
		hash[node.val] = new Node(node.val);
		for (Node sub : node.neighbors) {
			hash[node.val].neighbors.add(clone(sub, hash));
		}
		return hash[node.val];
	}

	@Override
	public void debug1() {
		Node node0 = new Node(0);
		Node node1 = new Node(1);
		node0.neighbors.add(node1);
		node1.neighbors.add(node0);
		Node c1 = this.cloneGraph(node0);
		Node c2 = node0;
		System.out.println(c2.val);
		node0.val = 100;
//		System.out.println(node1.neighbors.get(0).val);
		System.out.println(c2.val);
//		System.out.println(c1.val);
		System.out.println(c2.neighbors.get(0).val);
		c2.neighbors.get(0).val = 30;
		System.out.println(c2.neighbors.get(0).val);
//		System.out.println(c1.neighbors.get(0).val);
		System.out.println(node1.val);
//		System.out.println(c2.val);
		System.out.println(c1.neighbors.get(0).val);
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
